package tw.zld369504.testmediaplayer;

import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.pili.pldroid.player.AVOptions;
import com.pili.pldroid.player.PLMediaPlayer;

import java.io.IOException;

import static tw.zld369504.testmediaplayer.GlobalConfig.url;

public class CustomPlayerActivity extends AppCompatActivity implements PLMediaPlayer.OnPreparedListener, PLMediaPlayer.OnErrorListener,
        PLMediaPlayer.OnInfoListener, PLMediaPlayer.OnCompletionListener {

    private SurfaceView surfaceView;
    private SurfaceHolder surfaceHolder;
    private PLMediaPlayer mMediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_player);
        mMediaPlayer = new PLMediaPlayer(CustomPlayerActivity.this, createPlayerConfig());
        surfaceView = (SurfaceView) findViewById(R.id.surfaceView);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(new OnSurfaceCallBack());
    }

    @Override
    public void onPause() {
        super.onPause();
        mMediaPlayer.pause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mMediaPlayer.stop();
        mMediaPlayer.reset();
        mMediaPlayer.release();
    }

    private AVOptions createPlayerConfig() {
        AVOptions options = new AVOptions();
        // 当前播放的是否为在线直播，如果是，则底层会有一些播放优化
        // 默认值是：0
        options.setInteger(AVOptions.KEY_LIVE_STREAMING, 1);
        // 是否开启"延时优化"，只在在线直播流中有效
        // 默认值是：0
        options.setInteger(AVOptions.KEY_DELAY_OPTIMIZATION, 1);
        // 是否自动启动播放，如果设置为 1，则在调用 `prepareAsync` 或者 `setVideoPath` 之后自动启动播放，无需调用 `start()`
        // 默认值是：1
        options.setInteger(AVOptions.KEY_START_ON_PREPARED, 0);
        return options;
    }

    @Override
    public void onCompletion(PLMediaPlayer plMediaPlayer) {
        AlertDialog.Builder dialog =new AlertDialog.Builder(this);
        dialog.setTitle("Hint");
        dialog.setMessage("播放已经结束");
        dialog.setPositiveButton("ok",null).create().show();
    }

    private class OnSurfaceCallBack implements SurfaceHolder.Callback {

        @Override
        public void surfaceCreated(SurfaceHolder holder) {
            try {

                mMediaPlayer.setDataSource(url);
            } catch (IOException e) {
                e.printStackTrace();
            }
            mMediaPlayer.setDisplay(surfaceHolder);
            mMediaPlayer.setOnPreparedListener(CustomPlayerActivity.this);
            mMediaPlayer.setOnErrorListener(CustomPlayerActivity.this);
            mMediaPlayer.setOnInfoListener(CustomPlayerActivity.this);
            mMediaPlayer.setOnCompletionListener(CustomPlayerActivity.this);
            mMediaPlayer.prepareAsync();
        }

        @Override
        public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

        }

        @Override
        public void surfaceDestroyed(SurfaceHolder holder) {
            mMediaPlayer.stop();
            mMediaPlayer.reset();
        }
    }

    @Override
    public void onPrepared(PLMediaPlayer plMediaPlayer) {
        mMediaPlayer.start();
    }

    @Override
    public boolean onError(PLMediaPlayer plMediaPlayer, int i) {
        switch (i) {
            case PLMediaPlayer.ERROR_CODE_IO_ERROR:
                Log.d("234dsf", "ERROR_CODE_IO_ERROR网络异常");
                plMediaPlayer.stop();
                plMediaPlayer.reset();
                return true;
            case PLMediaPlayer.ERROR_CODE_404_NOT_FOUND:
                Log.d("234dsf", "ERROR_CODE_404_NOT_FOUND播放资源不存在");
                break;
            //ERROR_CODE_READ_FRAME_TIMEOUT
        }
        return false;
    }

    @Override
    public boolean onInfo(PLMediaPlayer plMediaPlayer, int i, int i1) {
        switch (i) {
            case PLMediaPlayer.MEDIA_INFO_BUFFERING_START:
                Log.e("tag", "buff_start");
                break;
            case PLMediaPlayer.MEDIA_INFO_BUFFERING_END:
                Log.e("tag", "buff_end");
                break;
        }
        return true;
    }
}
